//
//  CATest3ViewController.m
//  qiche
//
//  Created by liu haley on 12-12-1.
//  Copyright (c) 2012年 iCar. All rights reserved.
//

#import "CATest3ViewController.h"
#import "SensorsService.h"

@interface CATest3ViewController ()
{
    NSMutableArray *_titleArray;
    UILabel *_tableHeaderLabel;
    NSMutableArray *_titleValue;
    NSMutableArray *data4plot1;
}
@property (retain, nonatomic) NSMutableArray *data4plot1;
@end

@implementation CATest3ViewController
@synthesize chart,tableView;
@synthesize data4plot1;
- (void)dealloc
{
    if (_titleArray) {
        [_titleArray release];
    }
    if (_tableHeaderLabel) {
        [_tableHeaderLabel release];
    }
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"时速100-0公里刹车测试";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(pop)];
    
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc] initWithObjects:@"刹车前时速:",@"刹车距离:",@"刹车时间:",@"品牌:",@"车系:",@"车型:",@"年份:",@"当前里程:", nil]; 
    }
    
    if (!_titleValue) {
        _titleValue = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0", nil];
    }

    
    UIView *back = [[[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 175)] autorelease];
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"刹车网格300_175.png"]];
    back.backgroundColor = background;
    [background release];
    //back.alpha = 0.45;
    
    [self.view addSubview:back];
    //[self chartInit];
    
    [self performSelector:@selector(update) withObject:nil afterDelay:1.0];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void) update
{
    if ([SensorsService vehicleSpeed] > 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"必须在速度为0才能测试" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return ;
    }
    
    
    [self chartInit];
    
    while ([SensorsService vehicleSpeed] < [SensorsService maxVehicleSpeed] && [SensorsService vehicleSpeed]> 0) {
        [SensorsService testing:0.05];
        [data4plot1 addObject:[NSNumber numberWithFloat:[SensorsService vehicleSpeed]]];
        
        usleep(5000);
    }
    
    if([SensorsService vehicleSpeed] == 0) {
        [_titleValue replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%f",[SensorsService maxVehicleSpeed]]];
        [_titleValue replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%f",[SensorsService avgVehicleSpeed]]];
        
    }
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) pop {
    
    //[self dismissModalViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)chartInit
{
    self.chart.drawAxisX = NO;
    self.chart.drawAxisY = NO;
    self.chart.drawGridX = YES;
    self.chart.drawGridY = YES;
    
    self.chart.gridXColor = [UIColor whiteColor];
    self.chart.gridYColor = [UIColor whiteColor];
    
    //random init value for 3-plot
    data4plot1 = [[NSMutableArray alloc] initWithCapacity:10];
}
#pragma mark protocol S7GraphViewDataSource

- (NSUInteger)graphViewMaximumNumberOfXaxisValues:(S7GraphView *)graphView {
    return 10;
}

- (NSUInteger)graphViewNumberOfPlots:(S7GraphView *)graphView {
    /* Return the number of plots you are going to have in the view. 1+ */
    return 1;
}

- (NSArray *)graphViewXValues:(S7GraphView *)graphView {
    /* An array of objects that will be further formatted to be displayed on the X-axis.
     The number of elements should be equal to the number of points you have for every plot. */
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10], nil];
}

- (NSArray *)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex {
    /* Return the values for a specific graph. Each plot is meant to have equal number of points.
     And this amount should be equal to the amount of elements you return from graphViewXValues: method. */
    return self.data4plot1;
}

- (BOOL)graphView:(S7GraphView *)graphView shouldFillPlot:(NSUInteger)plotIndex {
    return NO;
}

#pragma mark- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)activeTableView numberOfRowsInSection:(NSInteger)section 
{
    if (_titleArray) {
        return [_titleArray count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tagStr = @"test3Cell";
    UITableViewCell *cell=[aTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@%d",tagStr,indexPath.row]];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%@%d",tagStr,indexPath.row]] autorelease];
    }
   // cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
    if([_titleValue objectAtIndex:indexPath.row] != @"0") {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[_titleArray objectAtIndex:indexPath.row] ,[_titleValue objectAtIndex:indexPath.row]];
    }
    else {
        cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
    }

    cell.textLabel.textColor = [UIColor brownColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    return cell;
}

#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return 35.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!_tableHeaderLabel) {
        _tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,self.tableView.frame.size.width, 30)];
        _tableHeaderLabel.backgroundColor=[UIColor redColor];
        _tableHeaderLabel.text = @"测试结果:";
        _tableHeaderLabel.textColor = [UIColor whiteColor];
        _tableHeaderLabel.font = [UIFont systemFontOfSize:14.0f];
    }
    return _tableHeaderLabel;
}

#pragma mark-Alert
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self pop];
}

@end
